# -*- coding:utf-8 -*-
# Author:凌逆战 | Never
# Date: 2023/6/28
"""

"""
import numpy as np
from scipy import signal
import sys

# 打印不使用科学计数法
np.set_printoptions(suppress=True)
# 精确到小数点后6位
np.set_printoptions(precision=8)

def getAsqrtAnalysisWindow(N, M, d):
    # filter_length, hop_length, d
    risingSqrtHann = np.sqrt(np.hanning(2 * (N - M - d) + 1)[:(N - M - d)])  # 上升
    fallingSqrtHann = np.sqrt(np.hanning(2 * M + 1)[:2 * M])  # 下降

    window = np.zeros(N)
    window[:d] = 0
    window[d:N - M] = risingSqrtHann[:N - M - d]
    window[N - M:] = fallingSqrtHann[-M:]

    return window


def getAsqrtSynthesisWindow(N, M, d):
    risingSqrtHann = np.sqrt(np.hanning(2 * (N - M - d) + 1)[:(N - M - d)])  # 上升
    fallingSqrtHann = np.sqrt(np.hanning(2 * M + 1)[:2 * M])  # 下降
    risingNoramlizedHann = np.hanning(2 * M + 1)[:M] / risingSqrtHann[N - 2 * M - d:N - M - d]

    window = np.zeros(N)
    window[:-2 * M] = 0
    window[-2 * M:-M] = risingNoramlizedHann
    window[-M:] = fallingSqrtHann[-M:]

    return window

frame_len = 32
M = 32
window_len = 512
AsqrtAnalysisWindow = getAsqrtAnalysisWindow(window_len, M, 0)
AsqrtSynthesisWindow = getAsqrtSynthesisWindow(window_len, M, 0)
# 150-200
print(AsqrtAnalysisWindow)
print(AsqrtSynthesisWindow)
# AsqrtAnalysisWindow = np.array2string(AsqrtAnalysisWindow, separator=',').replace(' ', '')
# AsqrtSynthesisWindow = np.array2string(AsqrtSynthesisWindow, separator=',').replace(' ', '')

print(AsqrtAnalysisWindow)
print(AsqrtSynthesisWindow)
# C的结果
AsqrtAnalysisWindow_C = [0.00000001, 0.00327249, 0.00654494, 0.00981732, 0.0130896, 0.01636173,
                                    0.01963369, 0.02290544, 0.02617695, 0.02944817, 0.03271908, 0.03598964,
                                    0.03925982, 0.04252957, 0.04579887, 0.04906767, 0.05233596, 0.05560368,
                                    0.0588708, 0.0621373, 0.06540313, 0.06866826, 0.07193265, 0.07519628,
                                    0.0784591, 0.08172107, 0.08498218, 0.08824237, 0.09150162, 0.09475989,
                                    0.09801714, 0.10127334, 0.10452846, 0.10778246, 0.11103531, 0.11428696,
                                    0.1175374, 0.12078657, 0.12403445, 0.127281, 0.13052619, 0.13376998,
                                    0.13701234, 0.14025323, 0.14349262, 0.14673047, 0.14996676, 0.15320143,
                                    0.15643447, 0.15966582, 0.16289547, 0.16612338, 0.1693495, 0.17257382,
                                    0.17579628, 0.17901686, 0.18223553, 0.18545224, 0.18866696, 0.19187967,
                                    0.19509032, 0.19829888, 0.20150532, 0.2047096, 0.20791169, 0.21111155,
                                    0.21430915, 0.21750446, 0.22069744, 0.22388805, 0.22707626, 0.23026205,
                                    0.23344536, 0.23662618, 0.23980446, 0.24298018, 0.24615329, 0.24932377,
                                    0.25249158, 0.25565668, 0.25881905, 0.26197864, 0.26513543, 0.26828937,
                                    0.27144045, 0.27458862, 0.27773385, 0.2808761, 0.28401534, 0.28715155,
                                    0.29028468, 0.2934147, 0.29654157, 0.29966528, 0.30278577, 0.30590302,
                                    0.30901699, 0.31212766, 0.31523498, 0.31833893, 0.32143947, 0.32453656,
                                    0.32763018, 0.33072029, 0.33380686, 0.33688985, 0.33996924, 0.34304499,
                                    0.34611706, 0.34918542, 0.35225005, 0.3553109, 0.35836795, 0.36142116,
                                    0.3644705, 0.36751594, 0.37055744, 0.37359497, 0.3766285, 0.379658,
                                    0.38268343, 0.38570477, 0.38872197, 0.39173501, 0.39474386, 0.39774847,
                                    0.40074883, 0.4037449, 0.40673664, 0.40972403, 0.41270703, 0.41568561,
                                    0.41865974, 0.42162938, 0.42459451, 0.42755509, 0.4305111, 0.43346249,
                                    0.43640924, 0.43935132, 0.44228869, 0.44522133, 0.44814919, 0.45107226,
                                    0.4539905, 0.45690388, 0.45981236, 0.46271592, 0.46561452, 0.46850814,
                                    0.47139674, 0.47428029, 0.47715876, 0.48003212, 0.48290034, 0.48576339,
                                    0.48862124, 0.49147386, 0.49432121, 0.49716327, 0.5, 0.50283138,
                                    0.50565737, 0.50847795, 0.51129309, 0.51410274, 0.5169069, 0.51970551,
                                    0.52249856, 0.52528602, 0.52806785, 0.53084403, 0.53361452, 0.53637929,
                                    0.53913832, 0.54189158, 0.54463904, 0.54738066, 0.55011642, 0.55284629,
                                    0.55557023, 0.55828823, 0.56100025, 0.56370626, 0.56640624, 0.56910015,
                                    0.57178796, 0.57446965, 0.57714519, 0.57981455, 0.5824777, 0.58513461,
                                    0.58778525, 0.5904296, 0.59306763, 0.5956993, 0.5983246, 0.60094349,
                                    0.60355594, 0.60616193, 0.60876143, 0.61135441, 0.61394084, 0.6165207,
                                    0.61909395, 0.62166057, 0.62422054, 0.62677382, 0.62932039, 0.63186022,
                                    0.63439328, 0.63691955, 0.639439, 0.6419516, 0.64445733, 0.64695615,
                                    0.64944805, 0.65193299, 0.65441095, 0.6568819, 0.65934582, 0.66180267,
                                    0.66425244, 0.66669509, 0.66913061, 0.67155895, 0.67398011, 0.67639405,
                                    0.67880075, 0.68120017, 0.6835923, 0.68597711, 0.68835458, 0.69072467,
                                    0.69308736, 0.69544264, 0.69779046, 0.70013081, 0.70246367, 0.704789,
                                    0.70710678, 0.70941699, 0.71171961, 0.7140146, 0.71630194, 0.71858162,
                                    0.7208536, 0.72311786, 0.72537437, 0.72762312, 0.72986407, 0.73209721,
                                    0.73432251, 0.73653994, 0.73874949, 0.74095113, 0.74314483, 0.74533057,
                                    0.74750833, 0.74967808, 0.75183981, 0.75399348, 0.75613908, 0.75827658,
                                    0.76040597, 0.7625272, 0.76464028, 0.76674516, 0.76884183, 0.77093027,
                                    0.77301045, 0.77508236, 0.77714596, 0.77920124, 0.78124818, 0.78328675,
                                    0.78531693, 0.7873387, 0.78935204, 0.79135693, 0.79335334, 0.79534126,
                                    0.79732065, 0.79929151, 0.80125381, 0.80320753, 0.80515265, 0.80708914,
                                    0.80901699, 0.81093618, 0.81284668, 0.81474848, 0.81664156, 0.81852588,
                                    0.82040144, 0.82226822, 0.82412619, 0.82597533, 0.82781563, 0.82964706,
                                    0.83146961, 0.83328326, 0.83508798, 0.83688375, 0.83867057, 0.8404484,
                                    0.84221723, 0.84397705, 0.84572782, 0.84746954, 0.84920218, 0.85092573,
                                    0.85264016, 0.85434547, 0.85604162, 0.85772861, 0.85940641, 0.86107501,
                                    0.86273439, 0.86438452, 0.8660254, 0.86765701, 0.86927932, 0.87089233,
                                    0.87249601, 0.87409034, 0.87567532, 0.87725091, 0.87881711, 0.8803739,
                                    0.88192126, 0.88345918, 0.88498764, 0.88650662, 0.8880161, 0.88951608,
                                    0.89100652, 0.89248743, 0.89395878, 0.89542056, 0.89687274, 0.89831532,
                                    0.89974828, 0.90117161, 0.90258528, 0.90398929, 0.90538362, 0.90676825,
                                    0.90814317, 0.90950837, 0.91086382, 0.91220953, 0.91354546, 0.91487161,
                                    0.91618796, 0.9174945, 0.91879121, 0.92007808, 0.92135511, 0.92262226,
                                    0.92387953, 0.92512691, 0.92636438, 0.92759194, 0.92880955, 0.93001722,
                                    0.93121493, 0.93240267, 0.93358043, 0.93474818, 0.93590593, 0.93705365,
                                    0.93819134, 0.93931898, 0.94043656, 0.94154407, 0.94264149, 0.94372882,
                                    0.94480605, 0.94587315, 0.94693013, 0.94797697, 0.94901365, 0.95004017,
                                    0.95105652, 0.95206268, 0.95305864, 0.9540444, 0.95501994, 0.95598526,
                                    0.95694034, 0.95788516, 0.95881973, 0.95974404, 0.96065806, 0.9615618,
                                    0.96245524, 0.96333837, 0.96421118, 0.96507367, 0.96592583, 0.96676764,
                                    0.96759909, 0.96842019, 0.96923091, 0.97003125, 0.97082121, 0.97160077,
                                    0.97236992, 0.97312866, 0.97387698, 0.97461487, 0.97534232, 0.97605933,
                                    0.97676588, 0.97746197, 0.9781476, 0.97882275, 0.97948742, 0.9801416,
                                    0.98078528, 0.98141846, 0.98204113, 0.98265328, 0.98325491, 0.98384601,
                                    0.98442657, 0.98499659, 0.98555606, 0.98610498, 0.98664333, 0.98717112,
                                    0.98768834, 0.98819498, 0.98869104, 0.98917651, 0.98965139, 0.99011567,
                                    0.99056934, 0.99101241, 0.99144486, 0.9918667, 0.99227791, 0.9926785,
                                    0.99306846, 0.99344778, 0.99381646, 0.9941745, 0.9945219, 0.99485864,
                                    0.99518473, 0.99550016, 0.99580493, 0.99609903, 0.99638247, 0.99665524,
                                    0.99691733, 0.99716875, 0.99740949, 0.99763955, 0.99785892, 0.99806761,
                                    0.99826561, 0.99845292, 0.99862953, 0.99879546, 0.99895068, 0.99909521,
                                    0.99922904, 0.99935216, 0.99946459, 0.99956631, 0.99965732, 0.99973764,
                                    0.99980724, 0.99986614, 0.99991433, 0.99995181, 0.99997858, 0.99999465,
                                    1, 0.99879546, 0.99518473, 0.98917651, 0.98078528, 0.97003125,
                                    0.95694034, 0.94154407, 0.92387953, 0.90398929, 0.88192126, 0.85772861,
                                    0.83146961, 0.80320753, 0.77301045, 0.74095113, 0.70710678, 0.67155895,
                                    0.63439328, 0.5956993, 0.55557023, 0.51410274, 0.47139674, 0.42755509,
                                    0.38268343, 0.33688985, 0.29028468, 0.24298018, 0.19509032, 0.14673047,
                                    0.09801714, 0.04906767]

AsqrtSynthesisWindow_C = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0.00242008,
                                   0.00965385, 0.02162715, 0.03822057, 0.05927058, 0.08457113, 0.11387566,
                                   0.14689945, 0.18332239, 0.22279203, 0.26492698, 0.30932056, 0.35554471,
                                   0.40315407, 0.45169023, 0.50068617, 0.54967067, 0.59817283, 0.64572659,
                                   0.69187513, 0.73617529, 0.77820178, 0.81755121, 0.85384598, 0.88673787,
                                   0.91591136, 0.94108661, 0.96202219, 0.97851732, 0.99041385, 0.99759771,
                                   1, 0.99879546, 0.99518473, 0.98917651, 0.98078528, 0.97003125,
                                   0.95694034, 0.94154407, 0.92387953, 0.90398929, 0.88192126, 0.85772861,
                                   0.83146961, 0.80320753, 0.77301045, 0.74095113, 0.70710678, 0.67155895,
                                   0.63439328, 0.5956993, 0.55557023, 0.51410274, 0.47139674, 0.42755509,
                                   0.38268343, 0.33688985, 0.29028468, 0.24298018, 0.19509032, 0.14673047,
                                   0.09801714, 0.04906767]

print(len(AsqrtAnalysisWindow), len(AsqrtAnalysisWindow_C))
print(len(AsqrtSynthesisWindow), len(AsqrtSynthesisWindow_C))
print(np.allclose(AsqrtAnalysisWindow, AsqrtAnalysisWindow_C, atol=1e-5))
print(np.allclose(AsqrtSynthesisWindow, AsqrtSynthesisWindow_C, atol=1e-5))
